//
//  ZBYVistor.swift
//  ZBYBlog
//
//  Created by zby on 16/2/24.
//  Copyright © 2016年 zby. All rights reserved.
//

import UIKit
import SnapKit

//定义一个协议
protocol ZBYVistorDelegate : NSObjectProtocol {
    
    //定义协议中的方法
    func didRegister()
    func didLogin()
    
}

class ZBYVistor: UIView {

    //设置代理属性
    weak var delegate: ZBYVistorDelegate?
    
    //重写init with frame方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //创建一个私有方法,来添加视图和设置约束
    func setUpUI(){
        
        //添加视图
        addSubview(homeIconImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(maskImageView)
        
        //设置约束
        homeIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeIconImageView.snp_bottom).offset(10)
            make.centerX.equalTo(self.snp_centerX)
            make.width.equalTo(216)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    }
    
    //懒加载
    //动画图片
    lazy var homeIconImageView: UIImageView = {
        let homeIconImageView = UIImageView()
        homeIconImageView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return homeIconImageView
    }()
    
    //通用图片
    lazy var iconImageView: UIImageView = {
        let iconImageView = UIImageView()
        iconImageView.image = UIImage(named: "visitordiscover_feed_image_house")
        return iconImageView
    }()
    
    //提示语
    lazy var messageLabel: UILabel = {
        let messageLabel = UILabel()
        messageLabel.font = UIFont.systemFontOfSize(14)
        messageLabel.textColor = UIColor.grayColor()
        messageLabel.textAlignment = .Center
        messageLabel.text = "关注一些人，回这里看看有什么惊喜"
        return messageLabel
    }()
    
    //注册按钮
    lazy var registerButton: UIButton = {
        let registerButton = UIButton(type: UIButtonType.Custom)
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        registerButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerButton.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return registerButton
    }()
    
    //登录按钮
    lazy var loginButton: UIButton = {
        let loginButton = UIButton(type: UIButtonType.Custom)
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        loginButton.setTitle("登录", forState: UIControlState.Normal)
        loginButton.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return loginButton
    }()
    
    //遮罩
    lazy var maskImageView: UIImageView = {
        let maskImageView = UIImageView()
        maskImageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return maskImageView
    }()
    
    //添加动画
    func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_2_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 5
        animation.removedOnCompletion = false
        
        //添加
        homeIconImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    //对外提供一个方法
    func setInfo(message: String,image: String,animation: Bool){
        messageLabel.text = message
        iconImageView.image = UIImage(named: image)
        //如果有动画
        if animation {
            //显示动画
            homeIconImageView.hidden = false
        }else{
            homeIconImageView.hidden = true
            //从性能上考虑可以移除动画
        }
    }
    
    //注册和登录按钮的点击事件
    func register(){
        self.delegate?.didRegister()
    }
    
    func login(){
        self.delegate?.didLogin()
    }
    
}








